/**
 * Container to process and hold information from an RSS entry.
 * Handles the non-uniform use of tags across RSS feeds.
 */

function RSSEntry(rssentry)
{
    this.title = rssentry['title'] || null;
    this.summary = rssentry['description'] || rssentry['summary'] || "-";
    this.summaryLen = (this.summary) ? this.summary.length : -1;
    this.image = "<img width='75' height='75' src='" + (    rssentry['image'] ||
                                                            rssentry['image66'] ||
                                                            rssentry['media:content'] ||
                                                            rssentry['media:group'] ||
                                                            'img/rss2.png'
                                                        ) + "'/>";  
    this.imageUrl = rssentry['image'] ||
                    rssentry['image66'] ||
                    rssentry['media:content'] ||
                    rssentry['media:group'] ||
                    'img/rss2.png';                                                     
    this.pubDate = rssentry['pubDate'] || rssentry['published'] || rssentry['updated'] || null;
    this.xmlPubDate = rssentry['xmlPubDate'] || rssentry['updated'] || null;
    this.link = rssentry['link'] || rssentry['guid'] || rssentry['id'] || null;
}

/**
 * Convert XML data into an array of JavaScript objects.
 */
function XML2JS(xmlDoc) {
    var output = new Array( );
    var root = getXMLRoot(xmlDoc);
    var rawData = xmlDoc.getElementsByTagName(root)[0];
    var i, j, oneRecord, oneObject;

	
    for (i = 0; i < rawData.childNodes.length; i++)
    {

        if (rawData.childNodes[i].nodeType == 1)
        {
            oneRecord = rawData.childNodes[i];
            oneObject = output[output.length] = new Object();
            var imageIndex = 0;
            for (j = 0; j < oneRecord.childNodes.length; j++)
            {
                if (oneRecord.childNodes[j].nodeType == 1)
                {
                    if (oneRecord.childNodes[j].tagName == "media:content" && imageIndex != 2)
                    {
                        oneObject[oneRecord.childNodes[j].tagName] = oneRecord.childNodes[j].attributes.getNamedItem("url").value;
                        imageIndex++;
                    }
                    else if (oneRecord.childNodes[j].firstChild != null) 
                        oneObject[oneRecord.childNodes[j].tagName] = oneRecord.childNodes[j].firstChild.nodeValue;
                }
            }
            
            if (rawData.childNodes[i].tagName == "pubDate")
                oneObject["xmlPubDate"] = rawData.childNodes[i].firstChild.nodeValue;
				
            if (!oneObject["xmlPubDate"] && rawData.childNodes[i].tagName == "updated")
                oneObject["xmlPubDate"] = rawData.childNodes[i].firstChild.nodeValue;
				
        }
        
    }

    return output;
}


function getXMLRoot(xmlDoc)
{
    var possibleRoots = [];
    possibleRoots.push("channel")
    possibleRoots.push("feed");

    for(i=0; i<possibleRoots.length; i++)
    {
        if(xmlDoc.getElementsByTagName(possibleRoots[i])[0] != null &&
            xmlDoc.getElementsByTagName(possibleRoots[i])[0] != undefined)
        {
            return possibleRoots[i];
        }
    }
}